package com.yy.jhl.ui.qrcode;

import android.content.Intent;
import android.hardware.Camera;
import android.os.Bundle;
import android.os.Vibrator;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.orhanobut.logger.Logger;
import com.yy.common.base.BaseActivity;
import com.yy.common.commonutils.StringUtil;
import com.yy.common.commonwidget.OnNoDoubleClickListener;
import com.yy.jhl.R;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bingoogolapple.qrcode.core.QRCodeView;
import cn.bingoogolapple.qrcode.zbar.ZBarView;

public class QRScanActivity extends BaseActivity implements QRCodeView.Delegate {
    public static final String EXTRA_QRCODE_SUCCESS = "EXTRA_QRCODE_SUCCESS";
    public static String qrcodeResult = "";
    @BindView(R.id.title_parent)
    LinearLayout titleParent;
    @BindView(R.id.zbarview)
    ZBarView mQRCodeView;
    @BindView(R.id.enter_code_text)
    TextView enterCodeText;
    @BindView(R.id.flashlights_text)
    TextView flashlightsText;
    @BindView(R.id.bottom_menu_parent)
    RelativeLayout bottomMenuParent;
    @BindView(R.id.code_edit)
    EditText codeEdit;
    @BindView(R.id.ensure_btn)
    Button ensureBtn;
    @BindView(R.id.enter_code_parent)
    LinearLayout enterCodeParent;
    private Camera camera = null;
    private boolean mIsLight = false;
    @Override
    public int getLayoutId() {
        return R.layout.activity_qr_scan;
    }

    @Override
    public void initView() {
        bottomMenuParent.getBackground().mutate().setAlpha(180);
        showTitle("扫一扫", new OnNoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                finish();
            }
        });
        mQRCodeView.setDelegate(this);
    }

    @Override
    public void finish() {

        super.finish();
    }

    @Override
    protected void onStart() {
        super.onStart();
        qrcodeResult = "";
        mQRCodeView.startCamera();
        mQRCodeView.showScanRect();
        mQRCodeView.startSpot();
    }

    @Override
    protected void onStop() {
        mQRCodeView.stopCamera();
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        mQRCodeView.onDestroy();
        super.onDestroy();
    }

    private void vibrate() {
        Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
        vibrator.vibrate(200);
    }

    @Override
    public void onScanQRCodeSuccess(String result) {
        //http://jiaohuilian.com/dl.html?p=766294513_2
        vibrate();
        mQRCodeView.stopSpot();
        mQRCodeView.stopCamera();
        try {
            String realResult = result.split("p=")[1].split("_")[0];
            qrcodeResult = realResult;
        }catch (Exception e){
            qrcodeResult = "***";
        }
        Intent intent = new Intent();
        intent.putExtra(EXTRA_QRCODE_SUCCESS, qrcodeResult);
        setResult(RESULT_OK, intent);
        finish();
    }

    /**
     * @return 扫描结果
     */
    public static String getQrcodeResult() {
        return qrcodeResult;
    }

    @Override
    public void onScanQRCodeOpenCameraError() {
        Logger.e("打开相机出错");
    }


    @OnClick({R.id.enter_parent, R.id.flashlights_parent,R.id.ensure_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.enter_parent:
                if (enterCodeParent.getVisibility() == View.VISIBLE){
                    enterCodeParent.setVisibility(View.GONE);
                }else {
                    enterCodeParent.setVisibility(View.VISIBLE);
                }
                break;
            case R.id.flashlights_parent:
                if (mIsLight){
                    closeLight();
                }else {
                    openLight();
                }
                break;
            case R.id.ensure_btn:
                String code = codeEdit.getText().toString().trim();
                if (StringUtil.isEmpty(code)){
                    showShortToast("箱号不能为空");
                    return;
                }
                mQRCodeView.stopSpot();
                mQRCodeView.stopCamera();
                qrcodeResult = code;
                Intent intent = new Intent();
                intent.putExtra(EXTRA_QRCODE_SUCCESS, qrcodeResult);
                setResult(RESULT_OK, intent);
                finish();
                break;
        }
    }
    private void  openLight(){
        if (camera == null) {
            camera = Camera.open();
        }
        camera.startPreview();
        Camera.Parameters parameters = camera.getParameters();
        List<String> flashModes = parameters.getSupportedFlashModes();
        if (flashModes == null) {
            return;
        }
        String flashMode = parameters.getFlashMode();
        if (!flashMode.contains(Camera.Parameters.FLASH_MODE_TORCH)) {
            parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
            camera.setParameters(parameters);
        }
        flashlightsText.setText("关闭手电筒");
        mIsLight = true;
    }
    private void  closeLight(){
        if (camera == null) {
            camera = Camera.open();
        }
        camera.startPreview();
        Camera.Parameters parameters = camera.getParameters();
        List<String> flashModes = parameters.getSupportedFlashModes();
        if (flashModes == null) {
            return;
        }
        String flashMode = parameters.getFlashMode();
        if (!flashMode.contains(Camera.Parameters.FLASH_MODE_OFF)) {
            parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
            camera.setParameters(parameters);
        }
        mIsLight = false;
        flashlightsText.setText("打开手电筒");
    }
}